package com.tjrac.zhaopin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.tjrac.zhaopin.bean.ListAllResumeResp;
import com.tjrac.zhaopin.entity.*;
import com.tjrac.zhaopin.mapper.EnterpriseInfoMapper;
import com.tjrac.zhaopin.service.EnterpriseInfoService;
import com.tjrac.zhaopin.service.JobListService;
import com.tjrac.zhaopin.service.PostInfoService;
import com.tjrac.zhaopin.service.SysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/enterprise")
public class EnterpriseController {
    @Autowired
    EnterpriseInfoService enterpriseInfoService;
    @Autowired
    EnterpriseInfoMapper enterpriseInfoMapper;
    @Autowired
    SysUserService sysUserService;
    @Autowired
    JobListService jobListService;
    @Autowired
    PostInfoService postInfoService;


    /**
     * 企业账户查询企业信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/getEnterpriseInfo")
    public R getEnterpriseInfo(Integer userId) {
        EnterpriseInfo enterpriseInfo = enterpriseInfoService.getOne(new QueryWrapper<EnterpriseInfo>().eq("holder_id", userId));
        if (enterpriseInfo != null) {
            return R.ok(enterpriseInfo);
        } else {
            return R.failed("失败！");
        }
    }

    /**
     * 完善企业信息
     *
     * @param enterpriseInfoReq
     * @return
     */
    @PostMapping("/fillEnterpriseInfo")
    @Transactional(rollbackFor = Exception.class)
    public R fillEnterpriseInfo(@RequestBody EnterpriseInfo enterpriseInfoReq) {
        QueryWrapper holderWrapper = new QueryWrapper<EnterpriseInfo>()
                .eq("holder_id", enterpriseInfoReq.getHolderId());
        try {
            EnterpriseInfo info = enterpriseInfoService.getOne(holderWrapper);
            if (info != null) {
                enterpriseInfoReq.setId(info.getId());
                enterpriseInfoService.updateById(enterpriseInfoReq);
            } else {
                enterpriseInfoService.save(enterpriseInfoReq);
            }
            return R.ok("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("失败");
        }
    }

    /**
     * 简历管理
     *
     * @param id
     * @return
     */
    @GetMapping("/listAllResume")
    public R listAllResume(@RequestParam String id) {
        List<ListAllResumeResp> listAllResumeResps = new ArrayList<>();
        List<JobList> jobLists = jobListService.list(new QueryWrapper<JobList>()
                .eq("holder_id", id));
        for (JobList jobList : jobLists) {
            List<HunterInfo> hunterInfos = enterpriseInfoMapper.listHunterInfoByJobId(jobList.getId());
            for (HunterInfo hunterInfo : hunterInfos) {
                ListAllResumeResp listAllResumeResp = new ListAllResumeResp();
                BeanUtils.copyProperties(hunterInfo, listAllResumeResp);
                listAllResumeResp.setJobName(jobList.getJobName());
                SysUser sysUser = sysUserService.getById(listAllResumeResp.getHunterId());
                if (sysUser != null) {
                    listAllResumeResp.setMobile(sysUser.getMobile());
                    listAllResumeResp.setEmail(sysUser.getEmail());
                    listAllResumeResp.setTruename(sysUser.getTruename());
                    listAllResumeResp.setJobId(jobList.getId());
                }
                listAllResumeResps.add(listAllResumeResp);
            }
        }
        return R.ok(listAllResumeResps);
    }

    /**
     * 管理简历
     *
     * @param hunterId
     * @param jobId
     * @param type     0 面试邀请 1 拒绝简历
     * @return
     */
    @GetMapping("/manageResume")
    @Transactional(rollbackFor = Exception.class)
    public R manageResume(@RequestParam String hunterId, @RequestParam String jobId, @RequestParam Integer type) {
        System.out.println(hunterId+","+jobId);
        PostInfo postInfo = postInfoService.getOne(new QueryWrapper<PostInfo>()
                .eq("hunter_id", hunterId)
                .eq("job_id", jobId));
        if (postInfo != null) {
            if (type == 0) {
                postInfo.setStatus(2);
            } else {
                postInfo.setStatus(1);
            }
            postInfoService.updateById(postInfo);
            return R.ok("管理完毕");
        } else return R.failed("该简历不存在");
    }


    @PostMapping("/postNewJob")
    @Transactional(rollbackFor = Exception.class)
    public R postNewJob(@RequestBody JobList jobList) {
        jobListService.save(jobList);
        return R.ok("成功");
    }

    @GetMapping("/listAllJob")
    public R listAllJob(@RequestParam String holderId) {
        return R.ok(jobListService.list(new QueryWrapper<JobList>().eq("holder_id", holderId)));
    }

    @GetMapping("/deleteJob")
    public R deleteJob(@RequestParam String id) {
        return R.ok(jobListService.removeById(id));
    }
}
